﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushJScript.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCore.css" />
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCoreEclipse.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>缓存的配置</h3>

    1.配置。在BaseController下声明静态Configure函数。该函数在项目加载时运行一次。
    <pre class="brush:csharp;">
        //.net framewok下父类为OwinAction
        //.net core下父类为CoreAction
        using NFinal;
        namespace SimpleWebApp
        {
            public BaseController : OwinAction//或者CoreAction
            {
                public override IDbConnection GetDbConnection()
                {
                    System.Data.IDbConnection con=new System.Data.SqlClient.SqlConnection("此处填写连接字符串");
                    return con;
                }
                /// &lt;summary>
                /// 该函数在项目加载时运行一次。
                /// &lt;/summary>
                public static void Configure(NFinal.Config.Plug.PlugConfig plugConfig)
                {
                    NFinal.Collections.FastDictionary&lt;string, StringContainer> keyValueDictionary = new NFinal.Collections.FastDictionary&lt;string, StringContainer>();
                    //此处可从数据库加载相关数据做为网站全局配置参数
                    keyValueDictionary.Add("siteName", "站点名称");
                    keyValueDictionary.Add("mobile","联系电话");
                    //快速搜索函数
                    plugConfig.keyValueCache = new NFinal.Collections.FastDictionary&lt;StringContainer>(keyValueDictionary);
                    systemConfigDictionary.Clear();
                }
            }
        }
    </pre>
    2.使用。
    <pre class="brush:csharp;">
        using NFinal;
        using Dapper;
        namespace SimpleWebApp.Controllers
        {
            public void Index()
            {
                this.ViewBag.Title="首页";
                this.ViewBag.Message="HelloWorld!";
                //不需要手动打开与关闭数据库连接。直接使用即可
                var users = this.con.QueryAll("select * from users");
                //使用缓存
                this.ViewBag.SiteName=this.config.keyValueCache["siteName"];
            }
        }
    </pre>
</body>
</html>